/*
 * 安卓弹框
 * */
var overlay = function(params) {
	var overlayBackground;//弹框背景
	var overlayDOM;//弹框对象
	
	var noop = function() {};//回调函数
	var defaults = {
		onbeforeshow: noop,
		onshow: noop,
		onbeforehide: noop,
		onhide: noop,
		text: "",
		icon: null,
		spinner: null,
		duration: null,
		id: null,
		parentEl: null
	};
	//是否支持 CSS3 transitions/animation
	var doesTransitions = (function() {
		var b = document.body || document.documentElement;
		var s = b.style;
		var p = 'transition';
		if (typeof s[p] === 'string') { return true; }

		// Tests for vendor specific prop
		var v = ['Moz', 'Webkit', 'Khtml', 'O', 'ms'];
		p = p.charAt(0).toUpperCase() + p.substr(1);
		for(var i=0; i<v.length; i++) {
			if (typeof s[v[i] + p] === 'string') { return true; }
		}
		return false;
	}());
	// helper - merge two objects together, without using $.extend
	// 合并参数
	var merge = function (obj1, obj2) {
		var obj3 = {};
		for (var attrOne in obj1) { obj3[attrOne] = obj1[attrOne]; }
		for (var attrTwo in obj2) { obj3[attrTwo] = obj2[attrTwo]; }
		return obj3;
	};
	//合并参数
	var settings = merge(defaults,params);
	
	//创建对象
	var create = (function() {
		//<div class="open-screen"></div>
		//<div class="open texC font14 animated">
		//	<h2>ni</h2><a href="javascript:;" class="open-close texC">×</a>
		//</div>
		overlayBackground = $('<div></div>');
		overlayBackground.addClass("open-screen");		
			
		overlayDOM = $('<div></div>');
		overlayDOM.addClass("open texC font14 animated");
		overlayDOM.append('<h2>' + settings.text + '</h2><a href="javascript:;" class="open-close texC">×</a>');
		
		//添加到body或父元素
		if (params.parentEl) {
			$(params.parentEl).append(overlayBackground);
			$(params.parentEl).append(overlayDOM);
		} else {
			$('body').append(overlayBackground);
			$('body').append(overlayDOM);
		}
		
		
		overlayBackground.onclick = hide;
		settings.onbeforeshow();
		// begin fade in
		overlayBackground.fadeIn(300);
		overlayDOM.addClass("animated").addClass("bounceIn").fadeIn();
		
		overlayDOM.find(".open-close").click(function(){
			hide();
		});
		overlayBackground.click(function(){
			hide();
		});
		
		if (settings.duration) {
			window.setTimeout(function() {
				hide();
			},settings.duration);
		}

	}());
	//消失
	var hide = function() {
		// pre-callback
		settings.onbeforehide();

		overlayBackground.fadeOut(200);
		overlayDOM.fadeOut(200, function(){
				destroy();
				settings.onhide();
			});
	}

	//消毁
	var destroy = function() {
		overlayBackground.remove();
		overlayDOM.remove();
		
	};
	
	return {
		hide: hide,
		destroy: destroy
	};
	
};
	
	
